
<template>
  <div class="big-container">
    <div class="create-container">
      <el-button type="primary" @click="openDialog">添加管道</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list">
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="管道编号" width="180" prop="pipeNumber" />
        <el-table-column label="管道直径" width="180" prop="pipeDiameter" />
        <el-table-column label="管道材质" width="120" prop="pipeMaterial" />
        <el-table-column label="管道种类" prop="pipeType">
        <template #default="scope">
            {{ formPipeType(scope.row.pipeType) }}
          </template>
        </el-table-column>

        <el-table-column label="管道状态">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>

        <el-table-column label="添加日期" prop="date" />
        <el-table-column label="添加时间" prop="time" />
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="sizes, prev, pager, next"
        :total="total"
        :page-sizes="[10, 20, 30, 40, 50]"
        :current-page="params.page"
        :page-size="params.pageSize"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <!-- 添加管道 -->
    <el-dialog
      title="添加管道"
      :visible="dialogVisible"
      :close-on-click-modal="false"
      width="580px"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="管道编号" prop="pipeNumber">
            <el-input v-model="addForm.pipeNumber" />
          </el-form-item>
          <el-form-item label="管道直径" prop="pipeDiameter">
            <el-input v-model="addForm.pipeDiameter" />
          </el-form-item>
          <el-form-item label="管道材质" prop="pipeMaterial">
            <el-input v-model="addForm.pipeMaterial" />
          </el-form-item>

          <el-form-item label="管道种类" prop="pipeType">
            <el-radio-group v-model="addForm.pipeType" size="small">
              <el-radio label="waterPipe" border>水管</el-radio>
              <el-radio label="conduit" border>电管</el-radio>
              <el-radio label="heatingPipe" border>暖气</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="管道状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="1">关闭</el-radio>
              <el-radio :label="0">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getAddPipeListAPI,createAddPipeListAPI } from '@/api/addpipe'
import { utils, writeFileXLSX } from 'xlsx'

export default {
  name: 'AddPipe',
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      roleList: [], // 角色列表
      addForm: {
        pipeNumber: '',
        pipeMaterial: '',
        pipeType: 'waterPipe',// 管道种类 waterPipe / conduit / heatingPipe
        status: '1',
        pipeDiameter: ''
      },
      addFormRules: {
        pipeNumber: [
          { required: true, message: '请输入管道编号', trigger: 'blur' }
        ],
        pipeDiameter: [
          { required: true, message: '请输入管道直径', trigger: 'blur' }
        ],
        pipeMaterial: [
          { required: true, message: '请输入管道材质', trigger: 'blur' }
        ],
        pipeType: [
          { required: true, message: '请选择管道种类', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择管道状态', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getAddPipeList()
  },
  methods: {
    // 管道种类
    formPipeType(pipeType) {
      const MAP = {
        waterPipe: '水管',
        conduit: '电管',
        heatingPipe: '暖气'
      }
      return MAP[pipeType]
    },
    async exportExcel() {
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表 要求一个对象数组格式
      // 1. 获取当前要导出的table数据
      const res = await getAddPipeListAPI(this.params)
      // 2. 按照需求调整数据的顺序
      const tableHeaderKeys = ['pipeNumber', 'pipeDiameter', 'pipeMaterial', 'pipeType', 'status','date' ,'time']
      const tableHeaderValues = ['管道编号', '管道直径', '管道材质', '管道种类', '管道状态', '添加日期','添加时间']
      // 以excel表格的顺序调整后端数据
      const sheetData = res.data.rows.map((item) => {
        const newObj = {} // 存放正确顺序的key-value
        tableHeaderKeys.forEach(key => {
          if (key === 'pipeType') {
            newObj[key] = this.formPipeType(item[key])
          } else {
            newObj[key] = item[key]
          }
        })
        return newObj
      })
      const worksheet = utils.json_to_sheet(sheetData)
      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, '管道数据.xlsx')
    },
    indexMethod(index){
      return (this.params.page -1)*this.params.pageSize + index + 1
    },
    // 获取管道列表
    async getAddPipeList() {
      const res = await getAddPipeListAPI(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 分页
    handleCurrentChange(val) {
      this.params.page = val
      this.getAddPipeList()
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getAddPipeList()
    },
    // 关闭弹框
    closeDialog() {
      this.dialogVisible = false
    },
    openDialog() {
      this.dialogVisible = true
    },

    // 确认添加
    async confirmAdd() {
        this.$refs.addForm.validate(async flag =>{
        if(!flag) return
        await createAddPipeListAPI(this.addForm)
        this.$message.success('添加成功！')
        this.getAddPipeList()
        this.closeDialog()
      })
    },
    // 适配状态
    formatStatus(status) {
      return status === '1' ? '关闭' : '启用'
    }
  }
}
</script>

<style lang="scss" scoped>
.big-container {
  padding: 20px;
  background-color: #fff;
}


.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
